package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Dict;
import cn.edu.nit.sys.mapper.DictMapper;
import cn.edu.nit.sys.service.DictService;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @Author xiaobai
 * @Date 2022/10/30 21:40
 * @Version 1.0.0
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public R addDict(Dict dict) {
        return baseMapper.insert(dict) > 0 ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R getList(String label, String value, Page<Dict> page) {
        Page<Dict> dictList = baseMapper.getList(label, value, page);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", dictList.getRecords());
        jsonObject.put("total", dictList.getTotal());
        return R.ok(jsonObject);
    }

    @Override
    public R updateDict(Dict dict) {
        return baseMapper.updateById(dict) > 0 ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R delDict(Integer id) {
        return baseMapper.deleteById(id) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }
}
